Java All-in-One For Dummies by Doug Lowe

Java All-in-One For Dummies by Doug Lowe

Author:Doug Lowe
Language: eng
Format: epub, pdf
Publisher: For Dummies
Published: 2011-07-31T16:00:00+00:00


The rest of this chapter shows you how to use these constructors and methods to work with ArrayList objects.

Creating an ArrayList Object

To create an array list, you first declare an ArrayList variable and then call the ArrayList constructor to instantiate an ArrayList object and assign it to the variable. You can do this on separate lines:

ArrayList signs;

signs = new ArrayList();

Alternatively, you can do it on a single line:

ArrayList signs = new ArrayList();

Here are a few things to note about creating array lists:

✦ The ArrayList class is in the java.util package, so your program must import either java.util.ArrayList or java.util.*.

• Unlike an array, an array list doesn’t make you specify a capacity — though you can if you want. Here’s a statement that creates an array list with an initial capacity of 100:

ArrayList signs = new ArrayList(100);

If you don’t specify a capacity for the array list, the initial capacity is set to 10. Providing at least a rough estimate of how many elements each array list can hold when you create it is a good idea.

✦ The capacity of an array list is not a fixed limit. The ArrayList class automatically increases the list’s capacity whenever necessary.

✦ If you’re using Java 1.5 or later, you can also specify the type of elements the array list is allowed to contain. This statement creates an array list that holds String objects:

ArrayList<String> signs = new ArrayList<String>();

The advantage of specifying a type when you declare an array list is that the compiler complains if you then try to add an object of the wrong type to the list. (This feature is called generics because it lets the Java API designers create generic collection classes that can be used to store any type of object. For more information, refer to Book IV, Chapter 5.)

• The ArrayList class also has a constructor that lets you specify another collection object (typically, another array list) whose items are copied into the new array list. This provides an easy way to make a copy of an array list, but you can also use it to convert any other type of collection to an array list.

Adding Elements

After you create an array list, you can use the add method to add objects to the array list. Here’s code that adds strings to an array list:

signs.add(“Drink Pepsi”);

signs.add(“No minors allowed”);

signs.add(“Say Pepsi, Please”);

signs.add(“7-Up: You Like It, It Likes You”);

signs.add(“Dr. Pepper 10, 2, 4”);

If you specified a type when you created the array list, the objects you add via the add method must be of the correct type.

You can insert an object at a specific position in the list by listing the position in the add method. Consider these statements:

ArrayList<String> nums = new ArrayList<String>();

nums.add(“One”);

nums.add(“Two”);

nums.add(“Three”);

nums.add(“Four”);

nums.add(2, “Two and a half”);

After these statements execute, the nums array list contains the following strings:

One

Two

Two and a half

Three

Four

Here are some important points to keep in mind when you add elements to array lists:

✦ If an array list is already at its capacity when you add an element, the array list automatically expands its capacity.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.